<template>
	<view class="">
		<j-cell-group>
			<j-cell title="打开" :more="false">
				<view slot="tips">
					<switch :checked="timiOpen" color="#FF0025" style="transform:scale(0.8)" @change="timiOpenChange">
					</switch>
				</view>
			</j-cell>
			<j-cell-card :key="index" :is-touch="compileMore" :show="compileMore" title="编辑" :more="compileMore">
				<j-cell-card title="星期" :show="false" :tips="weeklabel" :is-touch="false" :more="true">
					<checkbox-group @change="checkboxChange">
						<j-cell v-for="(item,index) in weekList" :key="index" :more="false" :title="item.name"
							:tips="item.tips">
							<view slot="icon">
								<checkbox :value="item.name" @click.stop="alarmChange(item)" :checked="item.status"
									style="transform:scale(0.7)" />
							</view>
						</j-cell>
					</checkbox-group>
				</j-cell-card>
				<picker mode="multiSelector" :value="timeValue" :range="[hours, minutes, hours, minutes]"
					@change="bindchangeTime">
					<j-cell title="有效时间" :tips="timelabel" :more="true">
					</j-cell>
				</picker>
				<j-cell title="标签" :tips="label" :more="false" />
			</j-cell-card>
		</j-cell-group>
		<view class="bottom-fixed">
			<button type="primary" class="block" plain @tap="save">保存</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				label: '时间段1',
				token: '',
				DetectMotionDetectList: [],
				timingType: '',
				timiOpen: false,
				timelabel: '',
				weeklabel: '',
				compileMore: false,
				timeValue: ['00', '00', '24', '00'],
				hours: ['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15',
					'16', '17', '18', '19', '20', '21', '22', '23', '24'
				],
				minutes: [
					'00',
					'01',
					'02',
					'03',
					'04',
					'05',
					'06',
					'07',
					'08',
					'09',
					'10',
					'11',
					'12',
					'13',
					'14',
					'15',
					'16',
					'17',
					'18',
					'19',
					'20',
					'21',
					'22',
					'23',
					'24',
					'25',
					'26',
					'27',
					'28',
					'29',
					'30',
					'31',
					'32',
					'33',
					'34',
					'35',
					'36',
					'37',
					'38',
					'39',
					'40',
					'41',
					'42',
					'43',
					'44',
					'45',
					'46',
					'47',
					'48',
					'49',
					'50',
					'51',
					'52',
					'53',
					'54',
					'55',
					'56',
					'57',
					'58',
					'59'
				],
				weekList: [{
					name: '周日',
					status: false,
					num: 0
				}, {
					name: '周一',
					status: false,
					num: 1
				}, {
					name: '周二',
					status: false,
					num: 2
				}, {
					name: '周三',
					status: false,
					num: 3
				}, {
					name: '周四',
					status: false,
					num: 4
				}, {
					name: '周五',
					status: false,
					num: 5
				}, {
					name: '周六',
					status: false,
					num: 6
				}, ],
				checkboxList: []
			}
		},
		onLoad(v) {
			this.timingType = v.timingType;
			this.token = v.token;
			this.getconfig()
			switch (v.timingType) {
				case '0':
					return this.label = '时间段1';
				case '1':
					return this.label = '时间段2'
				case '2':
					return this.label = '时间段3'
				case '3':
					return this.label = '时间段4'
				case '4':
					return this.label = '时间段5'
				default:
					break;
			}
		},
		methods: {
			getconfig() {
				uni.showLoading();
				let token = this.token;
				let data = {
					Name: 'Detect.MotionDetect'
				};
				getApp().globalData.api.getconfig(data, token, (result) => {
					uni.hideLoading()
					this.DetectMotionDetectList = result['Detect.MotionDetect'];
					this.weekFilter(result['Detect.MotionDetect'][0].EventHandler.TimeSection)
				});
			},
			// 星期间隔过滤
			weekFilter(value) {
				let arr = []
				let arr2 = []
				let arr3 = []
				let timiOpen = false
				let num = Number(this.timingType) + 1;
				let timelabel = ''
				let weeksName = '';
				this.checkboxList = []
				value.forEach((item, index) => {
					timelabel = item[num].substring(2, 19)
				})
				this.timeValue[0] = timelabel.substring(0, 2)
				this.timeValue[1] = timelabel.substring(3, 5)
				this.timeValue[2] = timelabel.substring(9, 11)
				this.timeValue[3] = timelabel.substring(12, 14)
				this.timelabel = timelabel
				arr2 = value.map((item, index) => {
					arr = item.map((ele, indez) => {
						ele = ele.slice(0, 1)
						return ele
					})
					arr.shift()
					return arr
				})
				console.log(arr2)
				arr3 = arr2.map((item, index) => {
					return item[this.timingType]
				})
				console.log(arr3)
				arr3.forEach((ele, indez) => {
					if (ele == '1') {
						timiOpen = true
						this.weekList[indez].status = true
						weeksName += this.weekList[indez].name;
						this.checkboxList.push(this.weekList[indez].num)
					}
				})
				this.timiOpen = timiOpen
				this.compileMore = timiOpen
				console.log(this.compileMore)
				this.weeklabel = weeksName
			},
			timiOpenChange(v) {
				this.timiOpen = v.detail.value
				this.compileMore = v.detail.value
			},
			// 时间段
			bindchangeTime(ev) {
				let value = ev.detail.value;
				let star = this.hours[value[0]] + ':' + this.minutes[value[1]] + ':00';
				let end = this.hours[value[2]] + ':' + this.minutes[value[3]] + ':00';
				let time = star + '-' + end;
				this.timelabel = time;
				let num = Number(this.timingType) + 1
				this.DetectMotionDetectList[0].EventHandler.TimeSection.forEach((item, index) => {
					console.log(item[num].substring(2, 19))
					item[num] = item[num].replace(item[num].substring(2, 19), time)
				})
				console.log(this.DetectMotionDetectList[0].EventHandler.TimeSection, '选择后的')
			},
			// 选择星期
			checkboxChange(v) {
				let weeksName = '';
				let arr = v.detail.value.map((item, index) => {
					let checknum
					this.weekList.forEach((ele, index) => {
						if (item == ele.name) {
							checknum = ele.num
							weeksName += ele.name
						}
					})
					return checknum
				})
				this.weeklabel = weeksName
				this.checkboxList = arr
				console.log(this.checkboxList)
			},
			// 星期点击选择框事件
			alarmChange(v) {
				console.log(v);
				this.weekList.forEach((ele, index) => {
					if (v.num == ele.num) {
						ele.status = !ele.status
					}
				})
			},
			save() {
				let arr = this.weekList.filter(item => item.status);
				console.log(arr)
				if (this.timiOpen && arr.length == 0) {
					console.log('没有星期')
					return uni.showToast({
						title: '请选择星期',
						icon: 'none'
					});
				} else {
					console.log('有星期')
					this.onSave();
				}
			},
			onSave() {
				console.log(this.timiOpen, '开关状态')
				uni.showLoading();
				let num = Number(this.timingType) + 1
				this.DetectMotionDetectList[0].EventHandler.TimeSection.forEach((item, index) => {
					this.checkboxList.forEach((ele, indez) => {
						if (ele == index) {
							console.log(item[num])
							if (this.timiOpen) {
								console.log('开')
								item[num] = item[num].replace(item[num].substring(0, 1), '1')
							} else {
								console.log('关')
								item[num] = item[num].replace(item[num].substring(0, 1), '0')
							}
						}
					})
				})
				console.log(this.DetectMotionDetectList[0].EventHandler.TimeSection, '提交的')
				let token = this.token;
				let data = {
					Name: 'Detect.MotionDetect',
					'Detect.MotionDetect': this.DetectMotionDetectList
				};
				getApp().globalData.api.setconfig(data, token, (result) => {
					uni.hideLoading()
					uni.showToast({
						title: '保存成功'
					});
					uni.navigateBack({
						delta: 1
					});
					var pages = getCurrentPages();
					var prePage = pages[pages.length - 2] //上一个页面
					console.log(prePage)
					if (prePage.route == "jvss/pagesDevice/device/device-live/setting/IPolice/alarmTime") {
						console.log(prePage.$vm)
						prePage.$vm.getconfig();
					}
				});
			}
		}
	}
</script>

<style>
</style>
